<!doctype html>
<html>    
	<head>        
		<meta charset="UTF-8">        
		<title></title>        
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />  
		<link href="../css/mui.min.css" rel="stylesheet" />         
		<style type="text/css">
			body {
				overflow: hidden;
			}

			.mui-bar {
				top: 0;
				height: 3rem;
				line-height: 2.8rem;
				color: white;
				background: #007AFF;
				;
			}

			.mui-bar h1 {
				color: white;
			}

			.code {
				position: absolute;
				top: 3rem;
				left: 0;
				width: 100%;
				height: 100%;
				background: #000000;
			}
		</style>    
	</head>    
	<body>        
		<header class="mui-bar">            
			<a class="mui-pull-left" onclick="goReturn()" style="color:white;">返回</a>  
			<h1 class="mui-title">二维码扫描</h1>            
			<span class="mui-spin mui-pull-right" onclick="scanPicture();">相册</span>  
			     </header>        
		<div id="code" class="code" style="">            
			<!--盛放扫描控件的div-->        
		</div>　　　　　　　　

		<script src="../js/mui.min.js"></script>
		<script src="../js/app.js"></script>

		<script type="text/javascript">
			mui.init({
				swipeBack: true //启用右滑关闭功能
			});
			var scan = null; //扫描对象              
			mui.plusReady(function() {
				startRecognize();
			});

			function startRecognize() {
				try {
					var styles = {
						frameColor: "#0000FF",
						scanbarColor: "#0000FF"
					}; //扫描控件构造                      
					scan = new plus.barcode.Barcode('code', null, styles);
					scan.onmarked = onmarked;
					scan.onerror = onerror;
					scan.start();
				} catch (e) {
					mui.alert("出现错误啦:\n" + e);
				}
			};

			function onerror(e) {
				miu.alert(e);
			};


			function onmarked(type, result) {
				if (type == 0) {
					//对扫描结果进行字符串拆分
					var res = result.split("wh_qrcode");
					if (res.length != 2) {
						alert(result);
					} else {
						var user = app.getUserGlobalInfo();
						var friendName = res[1];
						plus.nativeUI.showWaiting("请稍后");
						// 与后端联调
						mui.ajax(app.serverUrl + '/user/searchUsername?username=' + friendName, {
							type: 'post', //HTTP请求类型
							timeout: 10000, //超时时间设置为10秒；
							headers: {
								'Content-Type': 'application/json'
							},
							success: function(data) {
								// 关闭等待框
								plus.nativeUI.closeWaiting();
								if (data.status == 200) {
									mui.openWindow({
										url: "searchFriendsResult.html",
										id: "searchFriendsResult.html",
										extras: {
											friend: data.data
										}
									});

								} else {
									app.showToast(data.msg, "error");
								}
							}
						});
					}
				}
			};


			// 从相册中选择二维码图片               
			function scanPicture() {
				plus.gallery.pick(function(path) {
					plus.barcode.scan(path, onmarked, function(error) {
						plus.nativeUI.alert("无法识别此图片");
					});
				});
			} //返回关闭条码识别控件不然识别控件会一直显示            
			function goReturn() {
				scan.cancel();
				//结束条码识别                
				scan.close();
				//关闭条码识别控件                
				mui.back();
			}
			//子页面按键返回            
			document.addEventListener('plusready', function() {
				var webview = plus.webview.currentWebview();
				plus.key.addEventListener('backbutton', function() {
					scan.close();
					//关闭扫码条件                    
					webview.canBack(function(e) {
						if (e.canBack) {
							webview.back();
						} else {
							webview.close();
						}
					})
				});
			});
		</script>    
	</body>
</html>
